package com.csii.activiti.server.api;

import com.csii.activiti.server.api.vo.Result;
import com.csii.activiti.server.core.entity.*;
import com.csii.activiti.server.core.service.FlowOperationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Api(tags="流程操作接口")
@RestController
@RequestMapping("/${activity.basic.url:activiti}/flow/operation")
public class FlowOperationController {
    @Autowired
    private FlowOperationService flowOperationService;

    @ApiOperation("保存并发布流程")
    @PostMapping("/saveAndPublishFlow")
    public Result saveAndPublishFlow(@RequestBody ActFlowConfiguration flow){
        flowOperationService.saveAndPublishFlow(flow);
        return Result.ok();
    }

    @ApiOperation("完成任务节点")
    @PostMapping("/completeTask")
    public Result completeTask(@RequestBody ActCompleteTask actCompleteTask){
        flowOperationService.completeTask(actCompleteTask);
        return Result.ok();
    }

    @ApiOperation("完成任务节点通过实列ID")
    @PostMapping("/completeTaskByInstanceId")
    public Result completeTaskByInstanceId(@RequestBody ActProcessTaskByInstanceId actProcessTaskByInstanceId){
        flowOperationService.completeTaskByInstanceId(actProcessTaskByInstanceId);
        return Result.ok();
    }


    @ApiOperation("驳回任务节点通过实列ID")
    @PostMapping("/backTaskByInstanceId")
    public Result backTaskByInstanceId(@RequestBody ActProcessTaskByInstanceId actProcessTaskByInstanceId){
        flowOperationService.backTaskByInstanceId(actProcessTaskByInstanceId);
        return Result.ok();
    }

    @ApiOperation("驳回任务节点")
    @PostMapping("/backTask")
    public Result backTask(@RequestBody ActBackTask actBackTask){
        flowOperationService.backTask(actBackTask);
        return Result.ok();
    }

    @ApiOperation("委托他人代办")
    @PostMapping("/delegateTask")
    public Result delegateTask(@RequestBody ActDelegateTask actDelegateTask){
        flowOperationService.delegateTask(actDelegateTask);
        return Result.ok();
    }


    @ApiOperation("流程添加权限")
    @PostMapping("/addFlowPermission")
    public Result addFlowPermission(@RequestBody ActFlowPermission actFlowPermission){
        flowOperationService.addFlowPermission(actFlowPermission);
        return Result.ok();
    }

    @ApiOperation("保存流程")
    @PostMapping("/saveFlow")
    public Result saveFlow(@RequestBody ActFlowConfiguration flow){
        ActFlowConfiguration flowConfiguration = flowOperationService.saveFlow(flow);
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("id",flowConfiguration.getId());
        resultMap.put("flowKey",flowConfiguration.getFlowKey());
        return Result.ok(resultMap);
    }

    @ApiOperation("发布流程")
    @PostMapping("/publishFlow")
    public Result publishFlow(@ApiParam("流程ID") @RequestParam String flowId){
        flowOperationService.publishFlow(flowId);
        return Result.ok();
    }


    @ApiOperation("启动流程")
    @PostMapping("/startFlowInstanceByKey")
    public Result startFlowInstanceByKey(@RequestBody ActStartFlowInstance startFlowInstance){
        String instanceId = flowOperationService.startFlowInstanceByKey(startFlowInstance);
        return Result.ok((Object) instanceId);
    }


    @ApiOperation("启动流程iframe模式")
    @PostMapping("/startFlowInstanceIframeUrlByKey")
    public Result startFlowInstanceIframeUrlByKey(@RequestBody ActStartFlowInstanceIframeUrl startFlowInstanceIframeUrl){
        String instanceId = flowOperationService.startFlowInstanceByKey(startFlowInstanceIframeUrl);
        return Result.ok((Object) instanceId);
    }


    @ApiOperation("启动流程(无token)")
    @PostMapping("/startFlowInstanceByKeyNoToken")
    public Result startFlowInstanceByKeyNoToken(@RequestBody ActStartFlowInstanceIframeUrl startFlowInstanceIframeUrl){
        String instanceId = flowOperationService.startFlowInstanceByKey(startFlowInstanceIframeUrl);
        return Result.ok((Object) instanceId);
    }


    @ApiOperation("启动流程iframe模式(无token)")
    @PostMapping("/startFlowInstanceIframeUrlByKeyNoToken")
    public Result startFlowInstanceIframeUrlByKeyNoToken(@RequestBody ActStartFlowInstanceIframeUrl startFlowInstanceIframeUrl){
        String instanceId = flowOperationService.startFlowInstanceByKey(startFlowInstanceIframeUrl);
        return Result.ok((Object) instanceId);
    }


    @ApiOperation("流程排序")
    @PostMapping("/batchUpdateFlowSort")
    public Result batchUpdateFlowSort(@ApiParam("有顺序的主键")@RequestBody List<String> ids){
        flowOperationService.batchUpdateFlowSort(ids);
        return Result.ok();
    }


    @ApiOperation("完成消息节点")
    @PostMapping("/completeMessageTask")
    public Result completeMessageTask(@RequestBody ActMessageTask actMessageTask){
        flowOperationService.completeMessageTask(actMessageTask);
        return Result.ok();
    }

    @ApiOperation("驳回消息节点")
    @PostMapping("/backMessageTask")
    public Result backMessageTask(@RequestBody ActMessageTask actMessageTask){
        flowOperationService.backMessageTask(actMessageTask);
        return Result.ok();
    }

    @ApiOperation("移动到")
    @PostMapping("/moveToGroup")
    public Result moveToGroup(@RequestBody ActFlowMoveToGroup actFlowMoveToGroup){
        flowOperationService.moveToGroup(actFlowMoveToGroup);
        return Result.ok();
    }

    @ApiOperation("删除流程")
    @PostMapping("/deleteFlow")
    public Result deleteFlow(@RequestBody ActFlowCommonParam param){
        flowOperationService.deleteFlow(param.getFlowId());
        return Result.ok();
    }

    @ApiOperation("流程停用,启用")
    @PostMapping("/updatePublishStatus")
    public Result updatePublishStatus(@RequestBody ActFlowCommonParam param){
        flowOperationService.updatePublishStatus(param.getFlowId(),param.getGroupId());
        return Result.ok();
    }

    @ApiOperation("重新申请")
    @PostMapping("/reapply")
    public Result reapply(@RequestBody ActFlowCommonParam param){
        flowOperationService.reapply(param.getFlowId());
        return Result.ok();
    }

    @ApiOperation("撤回流程")
    @PostMapping("/withdrawalProcess")
    public Result withdrawalProcess(@RequestBody ActFlowWithdrawn actFlowWithdrawn){
        flowOperationService.withdrawalProcess(actFlowWithdrawn);
        return Result.ok();
    }

    @ApiOperation("手动流程催办")
    @PostMapping("/urgingProcessByInstanceId")
    public Result urgingProcessByInstanceId(@RequestBody ActFlowCommonParam flowCommonParam){
        flowOperationService.urgingProcessByInstanceId(flowCommonParam.getId());
        return Result.ok();
    }

    @ApiOperation("根据简单的密钥生成复杂密钥")
    @PostMapping("/generateComplexKey")
    public Result generateComplexKey(@RequestBody ActFlowCommonParam flowCommonParam){
        Object complexKey = flowOperationService.generateComplexKey(flowCommonParam.getCallbackKey());
        return Result.ok(complexKey);
    }

}
